'''
Created on Apr 4, 2012

@author: Vu Dao
'''
# Click on Create new lesson button
from selenium import webdriver
from Interface.LessonsPage import xpathOfCreateLesson, xpathOfTxtLessonName,\
    xpathOfComboLanguage, xpathOfCategories, xpathOfRadioButton, xpathOfActive,\
    xpathOfLessonCatalog, xpathTxtPrice, xpathBtnSubmitLesson,\
    xpathTxtSearchLesson, xpathDeleteLesson, xpathLessonLink
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException

def clickOnCreateLesson(self):
    browser = self.browser
    elem = browser.find_element_by_xpath(xpathOfCreateLesson())
    elem.click()
    browser.implicitly_wait(30)
    
# input value into all fields on table create lesson
def createLesson(self,nameLesson,language,categories, price, typeLesson = "Exclusively", typeDirectly = "Active",showLesson = "Show"):
    
    browser = self.browser
    
    elem=browser.find_element_by_xpath(xpathOfTxtLessonName())
    elem.clear()
        
    # Input lesson name
    elem = browser.find_element_by_xpath(xpathOfTxtLessonName())
    elem.send_keys(nameLesson)
    
    # Select Language
    elem = browser.find_element_by_xpath(xpathOfComboLanguage())
    Select(elem).select_by_visible_text(language)
    
    # Select categories
    elem = browser.find_element_by_xpath(xpathOfCategories())
    Select(elem).select_by_visible_text(categories)
    
    # check Exclusively through course
    if typeLesson == "Exclusively":
        elem = browser.find_element_by_xpath(xpathOfRadioButton(1))
        elem.click()
    else:
        # check Directly
        elem = browser.find_element_by_xpath(xpathOfRadioButton(2))
        elem.click()
        
        # check Show lesson in catalog
        if showLesson == "Show":
            elem = browser.find_element_by_xpath(xpathOfLessonCatalog())
#            elem.click()
            if elem.is_selected():
                pass
            else:
                elem.click()
        else:
            elem = browser.find_element_by_xpath(xpathOfLessonCatalog())
            if elem.is_selected():
                elem.click()
        
        # input price
        elem = browser.find_element_by_xpath(xpathTxtPrice())
        elem.send_keys(price)
    
    # check Active lesson
    if typeDirectly == "Active":
        elem = browser.find_element_by_xpath(xpathOfActive())
#        elem.click()
        if elem.is_selected():
            pass
        else:
            elem.click()
    else:
        elem = browser.find_element_by_xpath(xpathOfActive())
        if elem.is_selected():
            elem.click()
            
    
    # click on submit button
    elem = browser.find_element_by_xpath(xpathBtnSubmitLesson())
    elem.click()
    
    browser.implicitly_wait(30) 
    
    
# verify a lesson is created successfully
def verifyLessonCreatedSuccessfully(self,nameLesson):
    browser = self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser: browser.find_element_by_xpath("//a[contains(text(),'"+nameLesson+"')]"))
        return True
    except:
        return False

# input lesson name into search filed
def searchLesson(self,lessonName):
    browser = self.browser
    elem = browser.find_element_by_xpath(xpathTxtSearchLesson())
    elem.clear()
    elem.send_keys(lessonName) 
    elem.send_keys(Keys.RETURN) 
    
    browser.implicitly_wait(30) 
    
# Verify a lesson is searched
def isLessonSearch(self,lessonName):
    browser = self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser : browser.find_element_by_xpath("//span[contains(text(),'"+lessonName+"') and @class='resultsTitleBold']"))
        return True
    except:
        return False
   
# xpath delete a lesson 
def deleteLesson(self,lessonName):
#    browser = webdriver.Firefox()
    browser = self.browser
    elem = browser.find_element_by_xpath(xpathDeleteLesson(lessonName))
    elem.click()
    
    alert = browser.switch_to_alert()
    alert.accept()
    
    browser.refresh()
    browser.implicitly_wait(30)
    
# verify lesson is deleted
def verifyLessonDeleted(self,lessonName):
#    browser = webdriver.Firefox()
    browser = self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser : browser.find_element_by_xpath("//a[contains(text(),'"+lessonName+"')]"))
        return True
    except:
        return False

# click on lesson's link
def clickOnLessonLink(self,lessonLink):
    browser = self.browser
    elem = browser.find_element_by_xpath(xpathLessonLink(lessonLink))
    elem.click()
    browser.implicitly_wait(30)